/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rw.magallanes.ide.main;

import com.rw.magallanes.core.Item;
import com.rw.magallanes.core.Warehouse;
import com.rw.magallanes.db.Storage;
import com.rw.magallanes.ide.controller.ItemController;
import com.rw.magallanes.utilities.MessageType;
import com.rw.magallanes.utilities.Utilities;
import com.rw.table.ATableAction;
import com.rw.table.ATableModel;
import com.rw.table.editor.ICellEditor;
import com.rw.table.editor.TextCellEditor;
import com.rw.table.formatter.DoubleCellFormatter;
import com.rw.table.formatter.ICellFormatter;
import com.rw.table.formatter.StringCellFormatter;
import com.rw.table.render.ICellRenderer;
import com.rw.table.render.LabelCellRenderer;
import java.util.List;

/**
 *
 * @author Gonzalez
 */
public class ItemTableModel extends ATableModel implements ItemConstants {

    private List<Item> items;
    //
    private LabelCellRenderer labelCellRenderer = new LabelCellRenderer();
    //
    private TextCellEditor textEditor;
    //
    private StringCellFormatter strFormatter = new StringCellFormatter();
    private DoubleCellFormatter dblFormatter = new DoubleCellFormatter(2);

    public ItemTableModel() {
        Warehouse wh = Storage.getInstance().getWarehouse();

        items = wh.getAllItems();
        textEditor = new TextCellEditor();
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    @Override
    public ICellRenderer getRenderer(int row, int column) {
        return labelCellRenderer;
    }

    @Override
    public ICellEditor getEditor(int row, int column) {
        switch (column) {
            case COL_NAME:
                return textEditor;
            case COL_PRICE:
                return textEditor;
            default:
                throw new RuntimeException("Columna desconocida");
        }
    }

    @Override
    public ICellFormatter getFormatter(int row, int column) {
        switch (column) {
            case COL_NAME:
                return strFormatter;
            case COL_PRICE:
                return dblFormatter;
            default:
                throw new RuntimeException("Columna desconocida");
        }
    }

    @Override
    public ATableAction getEditableByClickAction(int row, int col) {
        return null;
    }

    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        if (value == null) {
            return;
        }

        MainFrame mf = MainSystem.getInstance().getMainFrame();
        Warehouse wh = mf.getWarehouse();

        Item item = this.items.get(rowIndex);
        switch (columnIndex) {
            case COL_NAME:
                ItemController.setName(item, (String) value);
                break;
            case COL_PRICE:
                ItemController.setPrice(item, ((Double) value).doubleValue());
                break;
        }
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public int getRowCount() {
        if (items != null) {
            return items.size();
        }
        return 0;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Item item = this.items.get(rowIndex);
        switch (columnIndex) {
            case COL_NAME:
                return item.getName();
            case COL_PRICE:
                return item.getPrice();
            default:
                throw new RuntimeException("Columna desconocida: " + columnIndex);
        }
    }

    Item getItem(int row) {
        return this.items.get(row);
    }
}
